
package ch.hearc.meteo.imp.afficheur.real.vue.atome;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;

import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;

import ch.hearc.meteo.imp.afficheur.real.JFrameStationMeteo;
import ch.hearc.meteo.imp.afficheur.real.moo.StationMeteoElement;

public class JPanelStationMeteoCarteElement extends JComponent implements MapMarker
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public JPanelStationMeteoCarteElement(StationMeteoElement stationMeteoElement)
		{
		this.stationMeteoElement = stationMeteoElement;
		paintCounter = 0;
		geometry();
		control();
		apparence();

		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	@Override
	public double getLat()
		{
		paintCounter++;
		if (paintCounter > 1)
			{
			this.setVisible(false);
			}
		else
			{
			this.setVisible(true);
			}
		return stationMeteoElement.getAffichageOption().getLatitude();
		}

	@Override
	public double getLon()
		{
		return stationMeteoElement.getAffichageOption().getLongitude();
		}

	@Override
	public void paint(Graphics g2d, Point point)
		{
		this.setLocation(point);
		this.setSize((int)(WIDTH * getScale()), (int)(HEIGHT * getScale()));
		g2d.setColor(JFrameStationMeteo.COLOR_S1);
		g2d.fillRect(point.x, point.y, (int)(WIDTH * getScale()), (int)(HEIGHT * getScale()));
		g2d.setColor(JFrameStationMeteo.COLOR_P1);
		g2d.drawRect(point.x, point.y, (int)(WIDTH * getScale()), (int)(HEIGHT * getScale()));
		g2d.setFont(new Font("Calibri", Font.BOLD, 20));
		paintCounter = 0;
		this.repaint();
		}

	@Override
	protected void paintComponent(Graphics g)
		{

		Graphics2D g2d = (Graphics2D)g;
		g2d.scale(getScale(), getScale());
		super.paintComponent(g2d);
		FontMetrics metrice = g2d.getFontMetrics();

		String txt = stationMeteoElement.getAffichageOption().getTitre();
		int y = (int)((metrice.getStringBounds(txt, g2d).getHeight()) + 2);

		g2d.drawString(txt, (CHECKBOX_WIDTH), y);
		txt = "T : " + stationMeteoElement.getStatTemperature().getLast();
		y += (int)((metrice.getStringBounds(txt, g2d).getHeight()));
		g2d.drawString(txt, (5), y);

		txt = "P : " + stationMeteoElement.getStatPression().getLast();
		y += (int)((metrice.getStringBounds(txt, g2d).getHeight()));
		g2d.drawString(txt, (5), y);

		txt = "A : " + stationMeteoElement.getStatAltitude().getLast();
		y += (int)((metrice.getStringBounds(txt, g2d).getHeight()));
		g2d.drawString(txt, (5), y);

		}

	public void refresh()
		{
		}

	public boolean isSelected()
		{
		return checkBox.isSelected();
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	public void setIsSelected(boolean isSelected)
		{
		checkBox.setSelected(isSelected);
		}

	@Override
	public void setEnabled(boolean isEnabled)
		{
		checkBox.setEnabled(isEnabled);
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public StationMeteoElement getStationMeteoElement()
		{
		return this.stationMeteoElement;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void apparence()
		{
		checkBox.setFocusable(false);
		this.setOpaque(false);
		}

	private void control()
		{
		checkBox.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					stationMeteoElement.getPanelStationMeteoListElement().setIsSelected(checkBox.isSelected());
					JFrameStationMeteo.getInstance().refreshSelectedStations(stationMeteoElement);
					}
			});
		this.addMouseListener(new MouseAdapter()
			{

				@Override
				public void mouseClicked(MouseEvent arg0)
					{
					checkBox.setSelected(!checkBox.isSelected());
					stationMeteoElement.getPanelStationMeteoListElement().setIsSelected(checkBox.isSelected());
					JFrameStationMeteo.getInstance().refreshSelectedStations(stationMeteoElement);

					}
			});
		}

	private void geometry()
		{
		this.setLayout(null);
		checkBox = new JCheckBox()
			{

				@Override
				protected void paintComponent(Graphics g)
					{
					Graphics2D g2d = (Graphics2D)g;
					double scale = getScale();
					g2d.scale(scale, scale);
					checkBox.setSize(new Dimension((int)(CHECKBOX_WIDTH * scale), (int)(CHECKBOX_WIDTH * scale)));
					super.paintComponent(g2d);
					}
			};
		checkBox.setLocation(1, 2);
		checkBox.setSize(new Dimension(CHECKBOX_WIDTH, CHECKBOX_WIDTH));
		checkBox.setBorder(BorderFactory.createEmptyBorder());

		this.add(checkBox, BorderLayout.CENTER);
		}

	private double getScale()
		{

		double scale = 0.5;
		if (JFrameStationMeteo.getInstance() != null)
			{
			scale += (JFrameStationMeteo.getInstance().getPanelCentral().getPanelMap().getZoom()) / 36.0;
			}
		return scale;
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	private StationMeteoElement stationMeteoElement;
	private int paintCounter;
	private JCheckBox checkBox;
	private static final int CHECKBOX_WIDTH = 20;
	private static final int WIDTH = 100;
	private static final int HEIGHT = 70;
	}
